# @Author: wei.yan
# @Email:13675196684@163.com
# @Date: 2022-06-22 17:14:07
# @Last Modified by: wei.yan
# @Last Modified time: 2022-06-22 17:14:07

# -*- coding=utf-8 -*-


# """
# 单个csv文件拆分
# """

import os
import pandas as pd

# 读取csv文件
data = pd.read_csv(r'E:\IpythonProject\DataSet1\aggr_5gsa_video_ciservice_day.csv')
savepath = os.path.dirname(r'E:\IpythonProject\DataSet1\aggr_5gsa_video_ciservice_day.csv')

data['clttime'] = pd.to_datetime(data['clttime'])
data['clttime'] = data['clttime'].apply(lambda x: x.strftime('%Y%m%d'))

# 获取clttime清单
clttime_range = data['clttime'].unique()
clttime_range = clttime_range.tolist()

# clttime_range
# type(clttime_range)


# 根据clttime进行文件拆分
for value in clttime_range:
    # print(value)
    data[data['clttime'] == value].to_csv(savepath + '\\'+ 'aggr_5gsa_video_ciservice_day_' + str(value) + '.csv',index = False, na_rep = 'N/A')
    print(f'{str(value)}输出成功')

# 进阶

# """遍历文件夹，进行csv文件拆分
# """
# import os
# import pandas as pd
#
#
#
# def scaner_file(real_url):
#     # 获取文件名字
#     real_filename = os.path.basename(real_url)
#     table_name = getTableName(real_filename)
#     #     print('table_name ' + table_name)
#
#     data = pd.read_csv(real_url)
#
#     # 将2000-1-1格式改为20000101
#     data['clttime'] = pd.to_datetime(data['clttime'])
#     data['clttime'] = data['clttime'].apply(lambda x: x.strftime('%Y%m%d'))
#
#     clttime_range = data['clttime'].unique()
#     clttime_range = clttime_range.tolist()
#
#     """
#     clttime_range = data['clttime'].unique()
#     # clttime_range = clttime_range.apply(lambda x:x.strftime('%Y-%m-%d %H:%M:%S'))  #时间转字符串
#     clttime_range = clttime_range.tolist()
#     """
#
#     for value in clttime_range:
#         # print(value)
#         # data[data['clttime'] == value].to_csv(table_name + '_' + str(value) + '.csv',index = False, na_rep = 'N/A')
#         data[data['clttime'] == value].to_csv(table_name + '_' + str(value) + '.csv', index=False,
#                                               na_rep='N/A')
#
#     # intoDB(real_url, table_name,conn)
#     # 打印出来
#     print(real_url)
#
# def getTableName(file_url):
#     _file_name = file_url  # 文件名称不带后缀
#     if '.' in file_url:
#         _file_name = file_url.split('.')[0]
#     return _file_name
#
# def get_suffix(file_url):
#     _suffix = ''  # 后缀
#     if '.' in file_url:
#         _suffix = file_url.split('.')[-1]
#     return _suffix
#

# scaner_file(r'E:\IpythonProject\DataSet1\aggr_5gsa_video_ciservice_day.csv')